package com.sw.haruka.module.title;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.sw.haruka.R;
import com.sw.haruka.dal.HarukaConfig;
import com.sw.haruka.view.FolderActivity;

/**
 * Created by Swsbty on 2021/05/05
 */
public class TransformerTitleFragment extends Fragment implements View.OnClickListener {

    private int mode = TitleMode.TITLE_BAR;

    private View mRootView;
    private ImageView mBackIv;
    private TextView mTitleIv;
    private ImageView mDoneIv;
    private MorePopupMenu mMorePopupMenu;

    private Context mContext;
    private IFolderDo mFolderDo;
    private IExplorerDo mExplorerDo;

    public TransformerTitleFragment(int mode) {
        this.mode = mode;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.title_fragment, container, false);
        mContext = getContext();
        mBackIv = mRootView.findViewById(R.id.back_iv);
        mBackIv.setOnClickListener(this);
        mTitleIv = mRootView.findViewById(R.id.title_tv);
        mDoneIv = mRootView.findViewById(R.id.done_iv);
        mDoneIv.setOnClickListener(this);
        initMode();
        return mRootView;
    }

    public void setMode(int mode) {
        this.mode = mode;
        initMode();
    }

    private void initMode() {
        switch (mode) {
            case TitleMode.FOLDER_BAR:
            {
                mBackIv.setImageResource(R.drawable.ic_arrow_left);
                mBackIv.setVisibility(View.VISIBLE);
                mTitleIv.setText(R.string.move_title);
                mTitleIv.setVisibility(View.VISIBLE);
                mTitleIv.setGravity(Gravity.CENTER_VERTICAL);
                mDoneIv.setVisibility(View.INVISIBLE);
                break;
            }
            case TitleMode.FTP_BAR:
            {
                mBackIv.setImageResource(R.drawable.ic_arrow_left);
                mBackIv.setVisibility(View.VISIBLE);
                mTitleIv.setText(getString(R.string.ftp_act_title));
                mTitleIv.setVisibility(View.VISIBLE);
                mTitleIv.setGravity(Gravity.CENTER_VERTICAL);
                mDoneIv.setVisibility(View.INVISIBLE);
                break;
            }
            case TitleMode.COLLECTION_BAR:
            {
                mBackIv.setImageResource(R.drawable.ic_arrow_left);
                mBackIv.setVisibility(View.VISIBLE);
                mTitleIv.setText(R.string.act_collection_title);
                mTitleIv.setVisibility(View.VISIBLE);
                mTitleIv.setGravity(Gravity.CENTER_VERTICAL);
                mDoneIv.setVisibility(View.INVISIBLE);
                break;
            }
            case TitleMode.SETTINGS_BAR:
            {
                mBackIv.setImageResource(R.drawable.ic_arrow_left);
                mBackIv.setVisibility(View.VISIBLE);
                mTitleIv.setText(R.string.settings);
                mTitleIv.setVisibility(View.VISIBLE);
                mTitleIv.setGravity(Gravity.CENTER_VERTICAL);
                mDoneIv.setVisibility(View.INVISIBLE);
                break;
            }
            case TitleMode.MOVE_BAR:
            {
                mBackIv.setImageResource(R.drawable.ic_arrow_left);
                mBackIv.setVisibility(View.VISIBLE);
                mTitleIv.setText(R.string.move_title);
                mTitleIv.setVisibility(View.VISIBLE);
                mTitleIv.setGravity(Gravity.CENTER_VERTICAL);
                mDoneIv.setImageResource(R.drawable.ic_paste);
                mDoneIv.setVisibility(View.VISIBLE);
                break;
            }
            case TitleMode.SELECT_BAR:
            {
                mBackIv.setImageResource(R.drawable.ic_clear);
                mBackIv.setVisibility(View.INVISIBLE);
                mTitleIv.setVisibility(View.VISIBLE);
                mTitleIv.setGravity(Gravity.CENTER);
                mTitleIv.setText(R.string.title_select_mode_hint);
                mDoneIv.setImageResource(R.drawable.ic_select_all);
                mDoneIv.setVisibility(View.VISIBLE);
                break;
            }
            case TitleMode.DOWNLOAD_MONITOR_BAR:
            {
                mBackIv.setImageResource(R.drawable.ic_arrow_left);
                mBackIv.setVisibility(View.VISIBLE);
                mTitleIv.setVisibility(View.VISIBLE);
                mTitleIv.setGravity(Gravity.CENTER_VERTICAL);
                mTitleIv.setText(R.string.download_monitor_title);
                mDoneIv.setImageResource(R.drawable.ic_history);
                mDoneIv.setVisibility(View.VISIBLE);
                break;
            }
            case TitleMode.TITLE_BAR:
            default:
            {
                mBackIv.setVisibility(View.INVISIBLE);
                mTitleIv.setVisibility(View.VISIBLE);
                mTitleIv.setText(getString(R.string.app_name));
                mTitleIv.setGravity(Gravity.CENTER);
                mDoneIv.setImageResource(R.drawable.ic_more);
                mDoneIv.setVisibility(View.VISIBLE);
                break;
            }
        }
    }

    public int getMode() {
        return mode;
    }

    @Override
    public void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.done_iv:
//            {
//                switch (mode) {
//                    case TitleMode.TITLE_BAR:
//                    {
//                        if (mMorePopupMenu == null) {
//                            mMorePopupMenu = new MorePopupMenu(mContext, mDoneIv);
//                        }
//                        mMorePopupMenu.show();
//                        break;
//                    }
//                    case TitleMode.DOWNLOAD_MONITOR_BAR:
//                    {
//                        startActivity(new Intent(mContext, FolderActivity.class).putExtra("path", HarukaConfig.Download_Path));
//                        break;
//                    }
//                    case TitleMode.MOVE_BAR:
//                    {
//                        mFolderDo.moveToThis();
//                        break;
//                    }
//                    case TitleMode.SELECT_BAR:
//                    {
//                        mExplorerDo.selectAll();
//                        break;
//                    }
//                }
//            }
//            case R.id.back_iv:
//            {
//                switch (mode) {
//                    case TitleMode.SELECT_BAR:
//                    {
//                        mExplorerDo.cancelSelect();
//                        break;
//                    }
//                    case TitleMode.DOWNLOAD_MONITOR_BAR:
//                    case TitleMode.FOLDER_BAR:
//                    case TitleMode.MOVE_BAR:
//                    case TitleMode.SETTINGS_BAR:
//                    case TitleMode.COLLECTION_BAR:
//                    case TitleMode.FTP_BAR:
//                    {
//                        getActivity().finish();
//                        break;
//                    }
//                }
//            }
//        }
    }

    public void setFolderDo(IFolderDo folderDo) {
        this.mFolderDo = folderDo;
    }

    public interface IFolderDo {
        void moveToThis();
    }

    public void setExplorerDo(IExplorerDo explorerDo) {
        this.mExplorerDo = explorerDo;
    }

    public interface IExplorerDo {
        void cancelSelect();
        void selectAll();
    }
}
